<template>
  <AppLayout title="选择角色" :show-back="true">
    <view class="role-select-container">
      <!-- 标题说明 -->
      <view class="section-header">
        <text class="section-title">选择您的角色</text>
        <text class="section-desc">请选择您在使用平台时的身份角色</text>
      </view>

      <!-- 角色卡片列表 -->
      <view class="role-list">
        <view
          v-for="role in roleOptions"
          :key="role.value"
          class="role-card"
          :class="{ 'role-card--selected': selectedRole === role.value }"
          @click="selectRole(role)"
        >
          <!-- 角色图标 -->
          <view class="role-icon-wrapper">
            <text class="role-icon">{{ role.icon }}</text>
          </view>

          <!-- 角色信息 -->
          <view class="role-info">
            <text class="role-name">{{ role.name }}</text>
            <text class="role-desc">{{ role.description }}</text>
          </view>

          <!-- 选中状态 -->
          <view class="role-selected-icon" v-if="selectedRole === role.value">
            <text class="selected-icon">✓</text>
          </view>
        </view>
      </view>

      <!-- 角色说明 -->
      <view class="role-notice">
        <view class="notice-item">
          <text class="notice-icon">ℹ️</text>
          <text class="notice-text">选择角色后将进入对应的功能界面</text>
        </view>
        <view class="notice-item">
          <text class="notice-icon">🔒</text>
          <text class="notice-text">不同角色拥有不同的权限和功能</text>
        </view>
      </view>

      <!-- 确认按钮 -->
      <view class="action-section">
        <FormButton
          text="确认选择"
          type="primary"
          size="large"
          :disabled="!selectedRole"
          block
          @click="confirmSelection"
        />
      </view>
    </view>
  </AppLayout>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { UserRole } from '@/utils/auth'
import { navigation } from '@/utils/navigation'
import { authGetters } from '@/store/modules/auth'
import AppLayout from '@/components/common/AppLayout.vue'
import FormButton from '@/components/common/FormButton.vue'

// 响应式数据
const selectedRole = ref<UserRole | null>(null)

// 角色选项
const roleOptions = [
  {
    value: UserRole.ADMIN_1,
    name: '超级管理员',
    description: '管理整个系统，拥有最高权限',
    icon: '👑',
    color: '#ff6b6b',
    features: ['用户管理', '系统配置', '数据统计', '权限分配']
  },
  {
    value: UserRole.ADMIN_2,
    name: '学校管理员',
    description: '管理本校师生和教学资源',
    icon: '🏫',
    color: '#4ecdc4',
    features: ['班级管理', '教师管理', '学生管理', '课程安排']
  },
  {
    value: UserRole.ADMIN_3,
    name: '班主任',
    description: '管理班级学生，记录学生表现',
    icon: '👨‍🏫',
    color: '#45b7d1',
    features: ['学生档案', '成绩管理', '表现记录', '家校沟通']
  },
  {
    value: UserRole.PARENT,
    name: '家长',
    description: '查看孩子在校情况和学习记录',
    icon: '👨‍👩‍👧‍👦',
    color: '#f7b731',
    features: ['成绩查看', '表现记录', '通知消息', '在线沟通']
  }
]

// 生命周期
onMounted(() => {
  // 如果已经登录，使用当前角色
  if (authGetters.currentRole.value) {
    selectedRole.value = authGetters.currentRole.value
  }
})

// 方法
/**
 * 选择角色
 */
const selectRole = (role: any) => {
  selectedRole.value = role.value

  // 触觉反馈
  uni.vibrateShort({
    type: 'light'
  })
}

/**
 * 确认选择
 */
const confirmSelection = () => {
  if (!selectedRole.value) return

  // 保存选择的角色
  uni.setStorageSync('selectedRole', selectedRole.value)

  // 显示选择成功提示
  uni.showToast({
    title: '角色选择成功',
    icon: 'success'
  })

  // 根据角色跳转到对应页面
  setTimeout(() => {
    switch (selectedRole.value) {
      case UserRole.ADMIN_1:
      case UserRole.ADMIN_2:
        navigation.toAdminDashboard()
        break
      case UserRole.ADMIN_3:
        navigation.toTeacherDashboard()
        break
      case UserRole.PARENT:
        navigation.toParentDashboard()
        break
      default:
        navigation.toHome()
    }
  }, 1000)
}
</script>

<style lang="scss" scoped>
.role-select-container {
  min-height: 100vh;
  padding: 20px;
  background: #f8f9fa;
}

.section-header {
  text-align: center;
  margin-bottom: 40px;

  .section-title {
    display: block;
    font-size: 24px;
    font-weight: 600;
    color: #333;
    margin-bottom: 8px;
  }

  .section-desc {
    display: block;
    font-size: 16px;
    color: #666;
    line-height: 1.5;
  }
}

.role-list {
  margin-bottom: 40px;
}

.role-card {
  display: flex;
  align-items: center;
  background: white;
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border: 2px solid transparent;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  cursor: pointer;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%);
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  }

  &:active {
    transform: translateY(0);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  }

  &--selected {
    border-color: v-bind('roleOptions.find(r => r.value === selectedRole)?.color || "#1890ff"');
    background: v-bind('roleOptions.find(r => r.value === selectedRole)?.color || "#1890ff"') + '10';

    &::before {
      opacity: 1;
    }

    .role-icon-wrapper {
      background: v-bind('roleOptions.find(r => r.value === selectedRole)?.color || "#1890ff"');
      color: white;
    }
  }

  .role-icon-wrapper {
    width: 64px;
    height: 64px;
    border-radius: 16px;
    background: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 20px;
    transition: all 0.3s ease;

    .role-icon {
      font-size: 32px;
    }
  }

  .role-info {
    flex: 1;

    .role-name {
      display: block;
      font-size: 18px;
      font-weight: 600;
      color: #333;
      margin-bottom: 4px;
    }

    .role-desc {
      display: block;
      font-size: 14px;
      color: #666;
      line-height: 1.4;
    }
  }

  .role-selected-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: v-bind('roleOptions.find(r => r.value === selectedRole)?.color || "#1890ff"');
    display: flex;
    align-items: center;
    justify-content: center;

    .selected-icon {
      color: white;
      font-size: 18px;
      font-weight: bold;
    }
  }
}

.role-notice {
  margin-bottom: 40px;

  .notice-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    background: white;
    border-radius: 8px;
    margin-bottom: 8px;
    border-left: 3px solid #1890ff;

    .notice-icon {
      font-size: 18px;
      margin-right: 12px;
    }

    .notice-text {
      flex: 1;
      font-size: 14px;
      color: #666;
      line-height: 1.4;
    }
  }
}

.action-section {
  padding: 0 20px;
}

// 响应式设计
@media (max-width: 480px) {
  .role-select-container {
    padding: 16px;
  }

  .role-card {
    padding: 20px 16px;

    .role-icon-wrapper {
      width: 56px;
      height: 56px;
      margin-right: 16px;

      .role-icon {
        font-size: 28px;
      }
    }

    .role-info {
      .role-name {
        font-size: 16px;
      }

      .role-desc {
        font-size: 13px;
      }
    }
  }

  .action-section {
    padding: 0;
  }
}

// 动画
.role-card {
  animation: fadeInUp 0.6s cubic-bezier(0.4, 0, 0.2, 1) backwards;

  &:nth-child(1) { animation-delay: 0.1s; }
  &:nth-child(2) { animation-delay: 0.2s; }
  &:nth-child(3) { animation-delay: 0.3s; }
  &:nth-child(4) { animation-delay: 0.4s; }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>